#include<iostream>
#include<queue>
using namespace std;

int n, m;
char maze[50][50];
bool vis[50][50];
int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};

struct node {
	int x, y, d;
	node (int this_x, int this_y, int this_d) {
		x = this_x;
		y = this_y;
		d = this_d;
	}
};

bool in(int x, int y) {
	return x >= 0 && x < n && y >= 0 && y < m;
}

int bfs(int x, int y) {
	vis[x][y] = true;
	queue<node> q;
	q.push(node(x, y, 0));
	while(!q.empty()) {
		node now = q.front();
		q.pop();
		for (int i = 0; i < 4; i++) {
			int dx = now.x + dir[i][0];
			int dy = now.y + dir[i][1];
			if (in(dx, dy) && maze[dx][dy] != '*' && !vis[dx][dy]) {
				if (maze[dx][dy] == 'T') {
					return now.d + 1;
				} else {
					vis[dx][dy] = true;
					q.push(node(dx, dy, now.d + 1));
				}
			}
		}
	}
	return -1;
}


int main(void) {
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> maze[i];
	}
	int start_x, start_y;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (maze[i][j] == 'S') {
				start_x = i, start_y = j;
				break;
			}
		}
	}
	cout << bfs(start_x, start_y) << endl;
	return 0;
}
